import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 导入 8 个动态路由模块
import approvalRouter from './modules/appvoval'
import attendanceRouter from './modules/attendance'
import deparmentRouter from './modules/department'
import employeeRouter from './modules/employee'
import permissionRouter from './modules/permission'
import roleRouter from './modules/role'
import salaryRouter from './modules/salary'
import socicalRouter from './modules/social'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * 动态路由表: 不是角色都可以访问的页面,需要权限控制
 */
export const asyncRoutes = [
  deparmentRouter,
  roleRouter,
  employeeRouter,
  permissionRouter,
  approvalRouter,
  attendanceRouter,
  salaryRouter,
  socicalRouter
]
/**
 * 静态路由表: 所有角色都可以访问的页面,不需要权限控制
 */
export const constantRoutes = [
  { // 登录
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  }, { // 404
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }, { // 主页
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: {
          title: '首页', // 菜单名称
          icon: 'dashboard'// 菜单的小图标
        }
      }
    ]
  }
]
// 重大发现：改了路由表，左侧菜单会随之变化，因为左侧菜单就是基于路由表动态循环生成的
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 注意：默认情况开放静态路由表，动态路由表需要经过筛选后追加进来
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

// 问题1: 路由表与左侧菜单的关系，为什么路由表变了，左侧菜单就变了？
// 答: 左侧菜单是基于路由表动态循环生成的

// 问题2: 为什么有的路由出现在了左侧，有些没有？
// 答: 受hidden属性的控制；true则不显示；false则显示
// 问题3: 哪些情况下生成的是一级菜单，哪些情况下生成的多级菜单？
// 答：3中情况会生成一级菜单；
// 情况1: 没有children，自身没有hidden为true的属性
// 情况2: 有children，但是children中只有唯一的孩子
// 情况3: 有children，但是只有一个需要显示的孩子
// 除了上述3中情况之外，都会生成多级菜单，原理是利用了组件递归生成的
